package sun.scheduler.core.job;

import sun.scheduler.core.scheduling.ResourceScheduler;

/**
 * A class used by the {@link ResourceScheduler} to compute the starttime in the
 * scheduling process.
 *
 * @author mfj
 */
public class SchedulingInfo extends Process
{
    /**
     * Gets the state of the calculation.
     */
    public final static Job.CurveState STATE = Job.CurveState.Scheduling;
    /**
     * The job which this instance belongs to.
     */
    public final Job baseJob;

    /**
     * Indicates the priority of the job.
     */
    public long previousEST = 0;
    /**
     * Indicates the priority of the job.
     */
    public long previousLST = 0;
    
    /**
     * Instantiates a new instance of the {@SchedulingInfo} class from the given
     * {@link RoundingInfo} instance after the rounding of the job resources was
     * completed.
     *
     * @param roundingInfo
     */
    public SchedulingInfo(RoundingInfo roundingInfo)
    {
        this.baseJob = roundingInfo.baseJob;
        updateStartTimes();
    }

    /**
     * Gets the duration of this job.
     *
     * @return the duration of this job.
     */
    @Override
    public long getDuration()
    {
        return baseJob.getDuration(Job.CurveState.Rounding);
    }

    @Override
    public double getAmountOfResources()
    {
        return baseJob.getAmountOfResources(Job.CurveState.Rounding);
    }

    public final void updateStartTimes()
    {
        RoundingInfo roundingInfo = (RoundingInfo) baseJob.getProcess(Job.CurveState.Rounding);
        setEarliestStartTime(roundingInfo.getEarliestStartTime());
        setLatestEndTime(roundingInfo.getLatestEndTime());
    }

    /**
     * Indicates wheter this job is critical.
     * @return {@code getLatestEndTime() - getDuration() == getEarliestStartTime()}
     */
    public boolean isCritical()
    {
        return getLatestEndTime() - getDuration() == getEarliestStartTime();
    }

    @Override
    public void setEarliestStartTime(long startTime)
    {
        previousEST = getEarliestStartTime();
        super.setEarliestStartTime(startTime);
    }

    @Override
    public void setLatestEndTime(long endTime)
    {
        previousLST = getLatestEndTime();
        super.setLatestEndTime(endTime);
    }

    /**
     * Reverts the last set of lst and est values.
     */
    public void resetTimes()
    {
        super.setEarliestStartTime(previousEST);
        super.setLatestEndTime(previousLST);
    }
}
